/* 
cd /projects/hsieh_project/proj_201809/code_1_data/
qstata --dofile=data_2_sales_cityind_top_mkt_sel.do --statatype=mp --cpucount=5 &
*/

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_data "${dir_proj}/data/"

global year1 = 1977
global year2 = 2013

global gl_perc = "10 1"
global gl_city = "msa1983 est"

/*
Author: Adarsh Kumar
Objective: Choose the appropriate start and terminal year for sales data. Eg: 

*REDACTED
1) Most industries would have start year = 1977, end year = 2012

However, some only have sales data starting from other years. Comments and code related to 
these different start/end years for different industries are excluded to abide by 
Census disclosure guidelines*/

/*
Relabel start and end year to 1977, 2013 to be compatible with LBD data calculations
*/

foreach i_city in $gl_city {
	di "`i_city'"
	
	use "${dir_data}/sales_cityind_top_mkt_`i_city'_sum", clear
	
	*Merge previously created xwalk to get min, max year to subset for each industry
	merge m:1 ch_ind using ${dir_data}/cw_year_sales 
	tab ch_ind if _merge != 3
	keep if _merge == 3
	drop _merge 
	
	rename year year_orig 
	gen year = year_orig 
	keep if year == year_min | year == year_max 
	replace year = ${year1} if year == year_min 
	replace year = ${year2} if year == year_max 
	
	save "${dir_data}/sales_cityind_top_mkt_`i_city'_sum_sel", replace
}


